package com.jx.service.content;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jx.dao.content.CategoryDao;
import com.jx.pojo.entity.content.Article;
import com.jx.pojo.entity.content.Category;
import com.jx.pojo.enums.content.CategoryTypeEnum;
import com.jx.pojo.po.content.CategoryPO;
import com.jx.pojo.po.content.CategoryTree;
import com.jx.pojo.po.content.SidebarMenuPO;
import com.jx.pojo.vo.content.CategoryVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

@Service
@Slf4j
public class CategoryService extends ServiceImpl<CategoryDao, Category> {



    /**
     * 获取菜单列表
     */
    public List<CategoryPO> listCategory(CategoryVO query) {
        // 查询出菜单列表
        QueryWrapper<Category> wrapper = new QueryWrapper<>();
        wrapper.like(StringUtils.isNotBlank(query.getCategoryName()), "menu_name", query.getCategoryName());
        wrapper.eq(query.getStatus() != null, "status", query.getStatus());
        List<Category> list = this.list(wrapper);

        // 按照级别进行分组
        List<CategoryPO> result = new ArrayList<>();
        // 目录一级
        for (Category category : list) {
            Integer categoryType = category.getType();
            if (CategoryTypeEnum.ONE.getValue().equals(categoryType)) {
                CategoryPO po = new CategoryPO();
                BeanUtils.copyProperties(category, po);
                result.add(po);
            }
        }
        if (!CollectionUtils.isEmpty(result)) {
            result = result.stream().sorted(Comparator.comparingInt(CategoryPO::getSort)).collect(Collectors.toList());
        }

        // 菜单一级
        Map<String, List<CategoryPO>> poMap = new HashMap<>();
        for (Category category : list) {
            Integer categoryType = category.getType();
            if (CategoryTypeEnum.TWO.getValue().equals(categoryType)) {
                CategoryPO po = new CategoryPO();
                BeanUtils.copyProperties(category, po);
                Integer parentId = category.getParentId();
                List<CategoryPO> twoCategoryList = poMap.get(parentId + "");
                if (CollectionUtils.isEmpty(twoCategoryList)) {
                    twoCategoryList = new ArrayList<>();
                }
                twoCategoryList.add(po);
                poMap.put(parentId + "", twoCategoryList);
            }
        }

        for (CategoryPO po : result) {
            List<CategoryPO> poList = poMap.get(po.getId() + "");
            if (CollectionUtils.isEmpty(poList)) {
                continue;
            }
            po.setChildren(poList.stream().sorted(Comparator.comparingInt(CategoryPO::getSort)).collect(Collectors.toList()));
        }
        return result;
    }

    public void saveCategory(Category category) {
        Integer id = category.getId();
        LocalDateTime dateTime = LocalDateTime.now();
        if (id == null) {
            category.setCreateTime(dateTime);
        }
        category.setUpdateTime(dateTime);
        this.saveOrUpdate(category);
    }

    /**
     * 获取目录列表
     */
    public List<Object> getDirList() {
        QueryWrapper<Category> wrapper = new QueryWrapper<>();
        wrapper.eq("type", CategoryTypeEnum.ONE.getValue());
        wrapper.orderByAsc("sort");
        List<Category> list = this.list(wrapper);
        List<Object> result = new ArrayList<>();
        Map<String, Object> map = new HashMap<>();
        map.put("value", 0);
        map.put("label", "主目录");
        result.add(map);
        for (Category category : list) {
            Map<String, Object> item = new HashMap<>();
            item.put("value", category.getId());
            item.put("label", category.getCategoryName());
            result.add(item);
        }
        return result;
    }

    /**
     * 获取菜单树
     */
    public List<CategoryTree> getTree() {
        // 查询出列表
        QueryWrapper<Category> wrapper = new QueryWrapper<>();
        wrapper.eq("status", 1);
        List<Category> list = this.list(wrapper);

        // 按照级别进行分组
        List<CategoryTree> result = new ArrayList<>();
        // 一级
        for (Category category : list) {
            Integer categoryType = category.getType();
            if (CategoryTypeEnum.ONE.getValue().equals(categoryType)) {
                CategoryTree po = new CategoryTree();
                po.setId(category.getId());
                po.setLabel(category.getCategoryName());
                po.setSort(category.getSort());
                result.add(po);
            }
        }
        if (!CollectionUtils.isEmpty(result)) {
            result = result.stream().sorted(Comparator.comparingInt(CategoryTree::getSort)).collect(Collectors.toList());
        }

        // 二级
        Map<String, List<CategoryTree>> twoCategoryMap = new HashMap<>();
        for (Category category : list) {
            Integer categoryType = category.getType();
            if (CategoryTypeEnum.TWO.getValue().equals(categoryType)) {
                CategoryTree po = new CategoryTree();
                po.setId(category.getId());
                po.setLabel(category.getCategoryName());
                po.setSort(category.getSort());
                Integer parentId = category.getParentId();
                List<CategoryTree> menuPOList = twoCategoryMap.get(parentId + "");
                if (CollectionUtils.isEmpty(menuPOList)) {
                    menuPOList = new ArrayList<>();
                }
                menuPOList.add(po);
                twoCategoryMap.put(parentId + "", menuPOList);
            }
        }

        for (CategoryTree po : result) {
            List<CategoryTree> menuPOList = twoCategoryMap.get(po.getId() + "");
            if (CollectionUtils.isEmpty(menuPOList)) {
                continue;
            }
            po.setChildren(menuPOList.stream().sorted(Comparator.comparingInt(CategoryTree::getSort)).collect(Collectors.toList()));
        }
        return result;
    }

    public List<Map<String, Object>> getOptions() {
        QueryWrapper<Category> wrapper = new QueryWrapper<>();
        wrapper.eq("status", 1);
        List<Category> list = this.list(wrapper);

        List<Category> oneLevelCategoryList = list.stream().filter(s -> s.getType() == 1).collect(Collectors.toList());
        List<Category> twoLevelCategoryList = list.stream().filter(s -> s.getType() == 2).collect(Collectors.toList());
        Map<Integer, List<Category>> twoLevelCategoryMap = twoLevelCategoryList.stream().collect(Collectors.groupingBy(Category::getParentId));
        List<Map<String, Object>> result = new ArrayList<>();
        for (Category category : oneLevelCategoryList) {
            Map<String, Object> item = new HashMap<>();
            item.put("label", category.getCategoryName());
            List<Category> categories = twoLevelCategoryMap.get(category.getId());
            List<Map<String, Object>> options = new ArrayList<>();
            for (Category c : categories) {
                Map<String, Object> option = new HashMap<>();
                option.put("value", c.getId());
                option.put("label", c.getCategoryName());
                options.add(option);
            }
            item.put("options", options);
            result.add(item);
        }
        return result;
    }

    public Map<Integer, String> getCategoryMap() {
        QueryWrapper<Category> wrapper = new QueryWrapper<>();
        wrapper.eq("status", 1);
        List<Category> list = this.list(wrapper);
        return list.stream().collect(Collectors.toMap(Category::getId, Category::getCategoryName));
    }


}
